package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.Time;
import util.db.MsSqlConn;
import factory.AkimimiOrderFactory;

public class AkimimiOrder {

	public static List<AkimimiOrderFactory> getAkimimiOrderList()
	{
		List<AkimimiOrderFactory> orderList = new ArrayList<AkimimiOrderFactory>();
		String sql = "select * from orders where id not in(select akimimi_order_id from Akimimi_order_mappings)";
		MsSqlConn mssqlConn = new MsSqlConn();
		ResultSet rs = mssqlConn.executeQuery(sql);
		
		int id = 0;
		String user_id = "";
		String out_trade_no = "";
		int delivery_mode = 0;
		int delivery_cost = 0;
		int other_cost = 0;
		int order_cost = 0;
		String invoice = "";
		int agent_area_id = 0;
		String agent_member_id = "";
		String area_agent_id = "";
		int pay_mode = 0;
		String pay_info = "";
		String delivery_name = "";
		String delivery_phone_1 = "";
		String delivery_phone_2 = "";
		int delivery_address_1 = 0;
		int delivery_address_2 = 0;
		int delivery_address_3 = 0;
		String delivery_detal_address = "";
		String delivery_email = "";
		int pay_status = 0;
		int status = 0;
		String create_time = "";
		String trade_finish_time = "";
		String update_id = "";
		String update_time = "";
		String coupon_name = "";
		int coupon_value = 0;
		String express_company = "";
		String express_info = "";
		int delivery_time = 0;
		String remark = "";
		int agent_delivery_id = 0;
		
		AkimimiOrderFactory order = null;
		
		try{
			while(rs.next())
			{
				try {
					id = rs.getInt("id");
				} catch (Exception e) {
					e.getMessage();
				}
				user_id = rs.getString("user_id");
				out_trade_no = rs.getString("out_trade_no");
				try {
					delivery_mode = rs.getInt("delivery_mode");
					delivery_cost = rs.getInt("delivery_cost");
					other_cost = rs.getInt("other_cost");
					order_cost = rs.getInt("order_cost");
					invoice = rs.getString("invoice");
					agent_area_id = rs.getInt("agent_area_id");
				} catch (Exception e) {
					e.getStackTrace();
				}
				agent_member_id = rs.getString("agent_member_id");
				area_agent_id = rs.getString("area_agent_id");
				try {
					pay_mode = rs.getInt("pay_mode");
				} catch (Exception e) {
					e.getStackTrace();
				}		
				pay_info = rs.getString("pay_info");
				delivery_name = rs.getString("delivery_name");
				delivery_phone_1 = rs.getString("delivery_phone_1");
				delivery_phone_2 = rs.getString("delivery_phone_2");
				try {
					delivery_address_1 = rs.getInt("delivery_address_1");
					delivery_address_2 = rs.getInt("delivery_address_2");
					delivery_address_3 = rs.getInt("delivery_address_3");
				} catch (Exception e) {
					e.getStackTrace();
				}
				delivery_detal_address = rs.getString("delivery_detal_address");
				delivery_email = rs.getString("delivery_email");
				try {
					pay_status = rs.getInt("pay_status");
					status = rs.getInt("status");
				} catch (Exception e) {
					e.getStackTrace();
				}
				create_time = rs.getString("create_time");
				trade_finish_time = rs.getString("trade_finish_time");
				update_id = rs.getString("update_id");
				update_time = rs.getString("update_time");
				coupon_name = rs.getString("coupon_name");
				try {
					coupon_value = rs.getInt("coupon_value");
				} catch (Exception e) {
					e.getStackTrace();
				}	
				express_company = rs.getString("express_company");
				express_info = rs.getString("express_info");
				try {
					delivery_time = rs.getInt("delivery_time");
				} catch (Exception e) {
					e.getStackTrace();
				}
				try {
					remark = rs.getString("remark");
				} catch (Exception e) {
					e.getStackTrace();
				}
				try {
					agent_delivery_id = rs.getInt("agent_delivery_id");
				} catch (Exception e) {
					e.getStackTrace();
				}	
				
				order = new AkimimiOrderFactory(id, user_id, out_trade_no, delivery_mode, delivery_cost, 
						other_cost, order_cost, invoice, agent_area_id, agent_member_id, area_agent_id,
						pay_mode, pay_info, delivery_name, delivery_phone_1, delivery_phone_2,
						delivery_address_1, delivery_address_2, delivery_address_3, delivery_detal_address,
						delivery_email, pay_status, status, create_time, trade_finish_time, update_id, update_time,
						coupon_name, coupon_value, express_company, express_info, delivery_time, remark, agent_delivery_id);
				orderList.add(order);
			}	
		}catch(SQLException e){
			System.out.println(e.getMessage() + e.getErrorCode());
		}
		
		mssqlConn.closeConn();
		
		System.out.println("[" + Time.nowTime() + "]: " + sql);
		
		return orderList;
	}
	
	
	public static int InsertOrderMappingLog(int a_id, int e_id)
	{
		String sql = "insert into Akimimi_order_mappings(akimimi_order_id, ebiz_order_id) " +
				"values (" + a_id + "," + e_id + ")";
		MsSqlConn mssqlConn = new MsSqlConn();
		int flag = mssqlConn.executeUpdate(sql);
		mssqlConn.closeConn();
		return flag;
	}
	
	public static int GetIdByName(String name)
	{
		int id = 0;
		if(name != null && !name.equals(""))
		{
			String sql = "select Regid from reg where Id='" + name + "'";
			System.out.println("[" + Time.nowTime() + "]: " + sql);
			MsSqlConn mssqlConn = new MsSqlConn();
			ResultSet rs = mssqlConn.executeQuery(sql);
			
			try{
				if(rs.next())
				{
					id = rs.getInt("Regid");
				}
			}catch(SQLException e){
				e.getErrorCode();
			}
			mssqlConn.closeConn();
		}
		return id;
	}
}
